﻿using System.IO;
using System.Runtime.Serialization.Json;
using System;

namespace Codeplex.Data.SerializeProvider
{
    public class DataContractJsonSerializeProvider : ISerializeProvider
    {
        public byte[] Serialize(Type type, object obj)
        {
            var serializer = new DataContractJsonSerializer(type);
            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                return ms.ToArray();
            }
        }

        public object Deserialize(Type type, byte[] bytes)
        {
            var serializer = new DataContractJsonSerializer(type);
            using (var ms = new MemoryStream(bytes))
            {
                return serializer.ReadObject(ms);
            }
        }
    }
}